/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import com.sun.tools.javac.util.Pair;
import dvd.modeles.Dvd;
import panier.modeles.Panier;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import panier.gestionnaires.GestionnairePanier;
import dvd.modeles.Dvd;
import dvd.gestionnaires.GestionnaireDvd;
import java.util.*;

/**
 *
 * @author Marcouss
 */
@WebServlet(name = "ServletPanier", urlPatterns = {"/ServletPanier"})
public class ServletPanier extends HttpServlet {
    @EJB
    private GestionnairePanier gestionnairePanier;
    @EJB
    private GestionnaireDvd gestionnaireDvd;
    double totalDvd = 0;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        HttpSession session = request.getSession(true);
        //String loginSession = session.getAttribute("log").toString();
        //Collection<Dvd> dvd = (Collection<Dvd>)session.getAttribute("listeDvd");//,listeDvd);
        ArrayList<Dvd> dvdA = (ArrayList<Dvd>)session.getAttribute("listeDvd");
        ArrayList<Integer> qttyA = (ArrayList<Integer>)session.getAttribute("listeQtty");
        //Collection<Map.Entry<Dvd,Integer>> dvd2 =null;
        session.setAttribute("totalDvd",""+ totalDvd);

        /*if(dvd==null){
            dvd = new ArrayList<Dvd>();
            session.setAttribute("listeDvd", dvd);
        }*/
        if(dvdA==null){
            dvdA = new ArrayList<Dvd>();
            qttyA = new ArrayList<Integer>();
            session.setAttribute("listeDvd", dvdA);
            session.setAttribute("listeQtty", qttyA);
        }
        
        String forwardTo = "";  
        String message = "";
        
        if (action != null) {
            //ajout d'un dvd au panier
            if (action.equals("buyDvd")) { 
                System.out.println("1");
                System.out.println(request.getParameter("price"));
                totalDvd += Double.parseDouble(request.getParameter("price")) ;
                System.out.println("2");
                session.setAttribute("totalDvd",""+  totalDvd);
                System.out.println("I'm in buyDvd");
                int idDvd = Integer.parseInt(request.getParameter("idDvd"));
                System.out.println(idDvd);
                Dvd d = gestionnaireDvd.getDvd(idDvd);
                
                System.out.println("getMarche");
                //dvd.add(d);
                
                dvdA.add(d);
                qttyA.add(1);
                
                
                //Map.Entry<Dvd,Integer> entry = new AbstractMap.SimpleEntry<Dvd, Integer>(d, 1);
                //dvd2.add(entry);
                
                //System.out.println("Liste = "+session.getAttribute("listeDvd"));
                //session.setAttribute("listeDvd", dvd);
                
                session.setAttribute("listeDvd", dvdA);
                session.setAttribute("listeQtty",qttyA);
                //System.out.println(dvd);
                //gestionnairePanier.addPanier(loginSession, idDvd);
                forwardTo = "monPanier.jsp?action=AfficherMonPanier"; 
            }
            else if (action.equals("removeDvd")) { 
                int idDvd = Integer.parseInt(request.getParameter("idDvd"));
                totalDvd -= Double.parseDouble(request.getParameter("price"));
                System.out.println("I'm in unbuy dvd");
                Dvd dremov = gestionnaireDvd.getDvd(idDvd);
                if(dvdA.contains(dremov)){
                    qttyA.remove(dvdA.indexOf(dremov));
                    dvdA.remove(dremov);
                    System.out.println("je delete un dvd gros");
                }
                //gestionnairePanier.removeFromPanier(loginSession, idDvd);
                forwardTo = "monPanier.jsp?action=AfficherMonPanier"; 
            }
            
            else if (action.equals("AfficherMonPanier")) { 
                System.out.println("I'm in getPanier");
                //Collection<Dvd> liste = (Collection<Dvd>)session.getAttribute("listeDvd");//gestionnairePanier.getMyPanier(loginSession); 
                
                Collection<Dvd> liste = (Collection<Dvd>)session.getAttribute("listeDvd");
                Collection<Integer> qtt = (Collection<Integer>)session.getAttribute("listeQtty");
                System.out.println(liste);
                request.setAttribute("listeDvd", liste);
                request.setAttribute("listeQtty", qtt);
                forwardTo = "monPanier.jsp?action=AfficherMonPanier"; 
            }
            
            else if (action.equals("addQtty")){
                System.out.println("In add qtty");
                int idDvd = Integer.parseInt(request.getParameter("idDvd"));
                System.out.println("idDvd = "+idDvd);
                Dvd d = gestionnaireDvd.getDvd(idDvd);
                int index = dvdA.indexOf(d);
                System.out.println("found index at : "+index);
                int quantite = qttyA.get(index);
                qttyA.set(index, quantite+1);
                session.setAttribute("listeQtty", qttyA);
                
                totalDvd += gestionnaireDvd.getDvd(idDvd).getPrix();
                session.setAttribute("totalDvd",""+  totalDvd);
                forwardTo = "monPanier.jsp";
            }
            
            
            else if (action.equals("removeQtty")){
                System.out.println("In remove Qtty");
                int idDvd = Integer.parseInt(request.getParameter("idDvd"));
                System.out.println(idDvd);
                Dvd d = gestionnaireDvd.getDvd(idDvd);
                int index = dvdA.indexOf(d);
                System.out.println("found index at : "+index);
                int quantite = qttyA.get(index);
                qttyA.set(index, quantite-1);
                session.setAttribute("listeQtty", qttyA);
                totalDvd -= gestionnaireDvd.getDvd(idDvd).getPrix();
                session.setAttribute("totalDvd",""+  totalDvd);
                forwardTo = "monPanier.jsp";
                
            }
            else if (action.equals("checkOut")) { 
                System.out.println("j'ai checkout");
                
                Boolean connected = (Boolean)session.getAttribute("connecte");
                System.out.println(connected);
                if(connected==null || !connected){
                    forwardTo = "connection.jsp";
                    
                }
                else{
                    for(Dvd d :dvdA){
                        System.out.println("Stock de "+d.getTitre()+" = "+d.getStock());
                        d.setStock(d.getStock()-qttyA.get(dvdA.indexOf(d)));
                        //gestionnaireDvd.deduceStockDvd(d.getId(), qttyA.get(dvdA.indexOf(d)));
                        System.out.println("Stock de "+d.getTitre()+" = "+d.getStock());
                    }
                    session.setAttribute("listeDvd",null);
                    session.setAttribute("listeQtty",null);
                    totalDvd=0;
                    session.setAttribute("totalDvd",""+totalDvd);
                    forwardTo = "monPanier.jsp?action=AfficherMonPanier"; 
                   
                }
            }
        }
        else{
        RequestDispatcher dp = request.getRequestDispatcher(forwardTo);  
        dp.forward(request, response); 
        }
        RequestDispatcher dp = request.getRequestDispatcher(forwardTo);  
        dp.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
